/*=============================================================================

  SYSTEM MANAGER APPLICATION MODULE

  FILE: SYSmgr_APP.cpp
  
  DESCRIPTION

    Application manager Application Operation Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <SYSmgr.h>

/*=============================================================================
  LOCALIZED VARIABLE DEFINITION
=============================================================================*/

/*=============================================================================
  EXTERNALIZED FUNCTION DEFINITION
=============================================================================*/
void SYSmgr::APP_Alloc(void)
{
	MGR_Alloc();
	MOD_Alloc();
	DEV_Alloc();
	EVT_Alloc();
	CFG_Alloc();
	m_manMode = 0;
	m_cycleCount = 0;
	m_cycleTotal = 0;
	m_homeReq = MOT_AXIS_ALL;
	m_sysWatch = Time_STAlloc();
}

void SYSmgr::APP_Free(void)
{
	MGR_Free();
	MOD_Free();
	DEV_Free();
	EVT_Free();
	CFG_Free();
}

void SYSmgr::ERR_REPORT(const char *title, const char *msg)
{
	Event_Pass(SYS_MGR, SYSTEM_ERR_E, (dword)title, (dword)msg);
	LOGERR("<%s>%s", title, msg);
	//Event_Handler(SYS_MGR, SYSTEM_ERR_E, (dword)title, (dword)msg);
}

void SYSmgr::NOTI_REPORT(const char *title, const char *msg)
{
	Event_Pass(SYS_MGR, SYSTEM_NOTI_E, (dword)title, (dword)msg);
	LOGWARN("<%s>%s", title, msg);
}

/*=============================================================================
  RESET OPERRATION 
=============================================================================*/
boolean SYSmgr::RESET_start(void)
{	
	switch(State_MajCurrent())
	{
	case RESET_S:
	{
		return	FALSE;
	}
	case IDL_S:
	{
		State_Change(RESET_S, RESET_IDL_S);
		State_SendCommand(RESET_START_C, 0, 0);
		break;
	}
	default:
	{
		RESET_clear();
		Stop();
		break;
	}
	}
	
	return	TRUE;
}

boolean SYSmgr::RESET_clear(void)
{
	State_SendCommand(RESET_CLEAR_C, 0, 0);

	return	TRUE;
}

boolean SYSmgr::HOME_start(dword dwAxis)
{
	if((State_MajCurrent() != IDL_S) && (State_MinCurrent() != IDL_IDL_S))
		return	FALSE;

	State_Change(RESET_S, RESET_IDL_S);
	State_SendCommand(RESET_HOME_C, dwAxis, 0);

	return	TRUE;
}

/*=============================================================================
  AUTO OPERATION
=============================================================================*/
boolean SYSmgr::AUTO_start(void)
{
	return	MANUAL_start(AREA_AUTO_M);
}

/*=============================================================================
  MANUAL OPERATION
=============================================================================*/
boolean SYSmgr::MANUAL_start(dword manMode)
{
	if((State_MajCurrent() != IDL_S) && (State_MinCurrent() != IDL_IDL_S))
			return	FALSE;
	
	switch(manMode)
	{
	case AREA_READ_M:
	{
		State_Change(MANUAL_S, MANUAL_IDL_S);
		State_SendCommand(MANUAL_START_C, 0, 0);
		break;
	}
	case AREA_AUTO_M:
	{
		State_Change(MANUAL_S, MANUAL_IDL_S);
		State_SendCommand(AUTO_START_C, 0, 0);
		break;
	}
	default:
	{
		m_manMode = AREA_NONE_M;
		return	FALSE;
	}
	}
	m_manMode = manMode;

	return	TRUE;
}

dword SYSmgr::MANUAL_current(void)
{
	if(State_MajCurrent() == IDL_S)
	{
		m_manMode = AREA_NONE_M;
	}
	
	return	m_manMode;
}

// TIME
void SYSmgr::WATCH_reset(void)
{
	if(m_sysWatch == NULL)
	{
		return;
	}
	Time_STReset(m_sysWatch);
	Time_STTick(m_sysWatch);
}

double64 SYSmgr::WATCH_get(void)
{	
	double64		elapse;

	if(m_sysWatch == NULL)
	{
		return	(0.0);
	}
	
	Time_STTock(m_sysWatch, &elapse);

	return	elapse;
}

void HIO_Delay(dword msec)
{
	int32		j,cnt;
	dword		res;

	if(msec <10)
	{
		Task_Delay(1);
		Task_Switch();
		return;
	}
	else if(msec <100)
	{
		cnt = (int32)msec/(int32)10;

		for(j=0; j=cnt; j++)
		{
			Task_Delay(10);
			Task_Switch();
		}
	}
	else
	{
		cnt = (int32)msec/(int32)100;
		res = msec%100;
		if(res != 0)
		{
			Task_Delay(res);
			Task_Switch();	
		}
		for(j=0; j<cnt; j++)
		{
			Task_Delay(100);
			Task_Switch();
		}
	}
}
/*--- END OF SYSmgr_Mod.CPP ---*/
